<?php
// $Id: theme.maintenance.inc 27 2010-11-16 12:49:54Z mjohnson60@gmail.com $

/**
 * _maintenance_theme()
 * 
 * Enter description here ...
 * 
 */
function _maintenance_theme() 
{
	global $theme;
	
	if (isset($theme)) 
	{
		return;
	}
	
	require_once('./includes/unicode.inc');
	require_once('./libraries/Router.php');
	require_once('./libraries/Theme.php');
	require_once('./libraries/Db.php');
	require_once('./libraries/Moduler.php');
	
	unicode_check();
	
	if (defined('CONFIGURATION_MODE') && (CONFIGURATION_MODE == 'INSTALL' || CONFIGURATION_MODE == 'UPDATE')) 
	{
		$theme = 'minnelli';
	}
	else 
	{
		if (!Db::isActive()) 
		{
			$module_list['system']['filename'] = 'modules/system/system.module';
			Moduler::moduleList(TRUE, FALSE, FALSE, $module_list);
			Loader::import('module', 'system');
		}
		
		$theme = Storage::get('maintenance_theme', 'minnelli');
	}
	
	$themes = Theme::listThemes();
	
	$theme_key = $theme;
	
		// Find all our ancestor themes and put them in an array.
	$base_theme = array();
	$ancestor = $theme;
	while ($ancestor && isset($themes[$ancestor]->base_theme))
	{
		$base_theme[] = $new_base_theme = $themes[$themes[$ancestor]->base_theme];
		$ancestor = $themes[$ancestor]->base_theme;
	}
	
	Theme::_init($themes[$theme], array_reverse($base_theme), array('Renderer', 'loadOfflineRegistry'));
	
	// These are usually added from system_init() -except maintenance.css.
	// When the database is inactive it's not called so we add it here.
	Athena::addCss(Athena::getPath('module', 'system') . '/defaults.css', 'module');
	Athena::addCss(Athena::getPath('module', 'system') . '/system.css', 'module');
	Athena::addCss(Athena::getPath('module', 'system') . '/system-menus.css', 'module');
	Athena::addCss(Athena::getPath('module', 'system') . '/maintenance.css', 'module');
}

/**
 * Return a themed list of maintenance tasks to perform.
 *
 * @ingroup themeable
 */
function render_task_list($items, $active = NULL)
{
	$done = isset($items[$active]) || $active == NULL;
	$output = '<ol class="task-list">';
	
	foreach ($items as $k => $item)
	{
		if ($active == $k)
		{
			$class = 'active';
			$done = false;
		}
		else
		{
			$class = $done ? 'done' : '';
		}
		
		$output .= '<li class="' . $class . '">' . $item . '</li>';
	}
	
	$output .= '</ol>';
	return $output;
}

/**
 * template_prerender_maintenance_page()
 * 
 * Enter description here ...
 * 
 * @param array $variables
 */
function template_prerender_maintenance_page(&$variables) 
{
	global $theme;
	// Retrieve the theme data to list all available regions.
	$theme_data = System::_themeData();
	$regions = $theme_data[$theme-name]->info['regions'];
	
	$variables['layout'] = 'none';
	if (!empty($variables['left'])) 
	{
		$variables['layout'] = 'left';
	}
	if (!empty($variables['right'])) 
	{
		$variables['layout'] = ($variables['layout'] == 'left') ? 'both' : 'right';
	}
	
	$page_title = page_title();
	if (!empty($page_title)) 
	{
		$head_title = array(strip_tags($page_title), Storage::get('site_name', 'Athena'));
	}
	else 
	{
		$head_title = array(Storage::get('site_name', 'Athena'));
		if (Storage::get('site_slogan', '')) 
		{
			$head_title[] = Storage::get('site_slogan', '');
		}
	}
	
	$variables['head_title'] = implode(' | ', $head_title);
	$variables['base_path'] = Config::basePath();
	$variables['breadcrumb'] = '';
	$variables['feed_icons'] = '';
	$variables['head'] = Athena::getHtmlHead();
	$variables['help'] = '';
	$variables['logo'] = '';
	$variables['messages'] = $variables['show_messages'] ? render('system_messages') : '';
	$variables['mission'] = '';
	$variables['primary_links'] = array();
	$variables['secondary_links'] = array();
	$variables['search_box'] = '';
	$variables['site_name'] = Storage::get('site_name', 'Athena');
	$variables['site_slogan'] = Storage::get('site_slogan', '');
	$variables['css'] = Athena::addCss();
	$variables['styles'] = Athena::getCss();
	$variables['scripts'] = Athena::getScripts();
	$variables['tabs'] = '';
	$variables['title'] = page_title();
	$variables['closure'] = '';
	
	$body_classes = array();
	$body_classes[] = 'configuration';
	if (isset($variables['db_is_active']) && !$variables['db_is_active']) 
	{
		$body_classes[] = 'db-offline';
		
		$variables['template_file'] = 'maintenance-page-offline';
	}
	
	if ($variables['layout'] == 'both') 
	{
		$body_classes[] = 'two-sidebars';
	}
	else if ($variables['layout'] == 'none') 
	{
		$body_classes[] = 'no-sidebars';
	}
	else 
	{
		$body_classes[] = 'one-sidebar sidebar-' . $variables['layout'];
	}
	
	$variables['body_classes'] = implode(' ', $body_classes);
}

/**
 * theme_install_page()
 * 
 * Enter description here ...
 * 
 * @param unknown_type $content
 * @return string
 */
function render_install_page($content)
{
	Athena::setHeader('Content-Type: text/html; charset=utf-8');
	
	$variables = array();
	$variables['content'] = $content;
	$variables['show_messages'] = FALSE;
	
	template_prerender_maintenance_page($variables);
	
	$messages = Athena::getMessages();
	if (isset($messages['error'])) 
	{
		$title = count($messages['error']) > 1 ? 'The following errors must be resolved before you can continue the installation process.' : 'The following error must be resolved before you can continue the installation process.';
		$variables['messages'] .= '<h3>' . $title . '</h3>';
		$variables['messages'] .= render('system_messages', 'error');
		$variables['content'] .= '<p>Please check the error messages and <a href="' . request_uri() . '">try again</a>.';
	}
	
	if (isset($messages['warning'])) 
	{
		$title = count($messages['warning']) > 1 ? 'The following installation warnings should be carefully reviewed.' : 'The following installation warning should be carefully reviewed.';
		$variables['messages'] .= '<h4>' . $title . ':</h4>';
		$variables['messages'] .= render('system_messages', 'warning');
	}
	
	// Special handling of status messages
	if (isset($messages['status']))
	{
		$title = count($messages['status']) > 1 ? 'The following installation warnings should be carefully reviewed, but in most cases may be safely ignored' : 'The following installation warning should be carefully reviewed, but in most cases may be safely ignored';
		$variables['messages'] .= '<h4>' . $title . ':</h4>';
		$variables['messages'] .= render('system_messages', 'status');
	}
	
	global $theme_path;
	$theme_path = 'themes/garland';
	
	return Renderer::renderTemplate('themes/garland/maintenance-page.tpl.php', $variables);
}
